﻿Imports DotNetNuke.Services.Scheduling
Imports DotNetNuke
Imports DotNetNuke.Common
Imports DotNetNuke.Data
Imports DotNetNuke.UI.Utilities.ClientAPI
Public Class view
    Inherits DotNetNuke.Entities.Modules.PortalModuleBase

    Private Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
        RegisterClientScriptBlock(Page, "jquery-ui.min.js", "<script src=""https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js""></script>")
        RegisterClientScriptBlock(Page, "jquery-ui.css", "<link rel=""stylesheet"" href=""http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/smoothness/jquery-ui.css"" type=""text/css"" media=""screen"" />")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtMessage.Text = ""
        If Not Page.IsPostBack Then
            InstallScheduleItem()
            PopulateRoles()
        End If
        ScheduleItemStatus()

    End Sub

    Private Sub PopulateRoles()
        Dim notifierSettings As DataTable = DataProvider.Instance().ExecuteDataSet("Blog_Notifier_Get_SettingsByPortal", PortalId).Tables(0)
        Dim rc As New DotNetNuke.Security.Roles.RoleController
        ddlRoles.DataSource = rc.GetPortalRoles(PortalId)
        ddlRoles.DataTextField = "RoleName"
        ddlRoles.DataValueField = "RoleId"

        If notifierSettings.Rows.Count > 0 Then
            ddlRoles.SelectedValue = notifierSettings.Rows(0).Item("notifyRoleId").ToString

            If notifierSettings.Rows(0).Item("emailSubject").ToString <> "" Then
                tbEmailSubject.Text = notifierSettings.Rows(0).Item("emailSubject").ToString
            End If

            If notifierSettings.Rows(0).Item("SenderEmail").ToString <> "" Then
                tbEmailAddress.Text = notifierSettings.Rows(0).Item("SenderEmail").ToString
            End If

        End If

        ddlRoles.DataBind()


       



    End Sub

    Private Sub InstallScheduleItem()
        If Not IsScheduleItemInstalled() Then
            Dim objScheduleItem As New ScheduleItem
            objScheduleItem.FriendlyName = "Blog Notifier"
            objScheduleItem.Enabled = True
            objScheduleItem.TypeFullName = "Spiffy.BlogNotifier.BlogNotifierScheduler"
            objScheduleItem.TimeLapse = "1"
            objScheduleItem.TimeLapseMeasurement = "m"
            objScheduleItem.RetryTimeLapse = "1"
            objScheduleItem.RetryTimeLapseMeasurement = "m"
            objScheduleItem.RetainHistoryNum = 100

            SchedulingProvider.Instance().AddSchedule(objScheduleItem)
        End If
    End Sub

    Private Function IsScheduleItemInstalled() As Boolean
        Dim scheduleItems As ArrayList = SchedulingProvider.Instance().GetSchedule()
        Dim isInstalled = False
        For Each s As ScheduleItem In scheduleItems
            If s.FriendlyName = "Blog Notifier" Then
                BlogNotifierScheduleItem = s
                Return True
            End If

        Next

        Return False

    End Function

    Private Sub ScheduleItemStatus()
        If BlogNotifierScheduleItem Is Nothing Then
            IsScheduleItemInstalled()
        End If

        If BlogNotifierScheduleItem.Enabled = False Then
            btnActivation.CssClass &= " spiffy_bn_activation_inactive"
            btnActivation.Text = "Notifier is Disabled"
        Else
            btnActivation.CssClass = "spiffy_bn_activation"
            btnActivation.Text = "Notifier is Enabled"
        End If

    End Sub

    Private Sub ToggleScheduleItem()
        If IsScheduleItemInstalled() Then
            If BlogNotifierScheduleItem.Enabled = True Then
                BlogNotifierScheduleItem.Enabled = False
            Else
                BlogNotifierScheduleItem.Enabled = True
            End If

            SchedulingProvider.Instance().UpdateSchedule(BlogNotifierScheduleItem)
            ScheduleItemStatus()
        End If
    End Sub

    Private Sub btnSave_Click(sender As Object, e As System.EventArgs) Handles btnSave.Click
        If ddlRoles.SelectedValue <> "" Then
            DataProvider.Instance().ExecuteScalar("Blog_Notifier_Update_NotifyRoleId", PortalId, ddlRoles.SelectedValue, tbEmailSubject.Text, tbEmailAddress.Text)
            txtMessage.Text = "Settings have been saved."
        End If

    End Sub

    Private _blogNotfierScheduleItem As ScheduleItem

    Private Property BlogNotifierScheduleItem As ScheduleItem
        Get
            Return _blogNotfierScheduleItem
        End Get
        Set(value As ScheduleItem)
            _blogNotfierScheduleItem = value
        End Set
    End Property

    Private Sub btnActivation_Click(sender As Object, e As System.EventArgs) Handles btnActivation.Click
        ToggleScheduleItem()
    End Sub
End Class